import torch
import torch.nn as nn
from models.VGG.VGGMain import VGG19
from utils import get_data_loaders

def evaluate_model():
    _, test_loader = get_data_loaders()  # 获取测试数据加载器

    model = VGG19().cuda()
    model.load_state_dict(torch.load('./best_model_AlexNet.pth'))
    model.eval()

    loss_fn = nn.CrossEntropyLoss()
    total_loss = 0
    correct = 0
    total = 0

    with torch.no_grad():
        for data in test_loader:
            imgs, labels = data
            imgs, labels = imgs.cuda(), labels.cuda()
            outputs = model(imgs)

            loss = loss_fn(outputs, labels)
            total_loss += loss.item()

            _, predicted = torch.max(outputs.data, 1)
            total += labels.size(0)
            correct += (predicted == labels).sum().item()

    average_loss = total_loss / len(test_loader)
    accuracy = 100 * correct / total

    print(f"测试集平均损失: {average_loss:.4f}, 测试集准确率: {accuracy:.2f}%")